local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerObject());
local ConnectClass=_G.CommonOHOH.load.Connect();
local EventList = ServerOHOH.load.EventList();
local ServerEventManager = ServerOHOH.load.ServerEventManager();

local PLAYER_LIST={}
function M.FindPlayerByUserId(userId)
	return PLAYER_LIST[userId]
end

function M.new(self)
	return M(self);
end;
function M.ctor(self, playerInst)

	_G.CommonOHOH.load.PlayerTool().CreateConnectEvent(self)

	M.super.ctor(self, playerInst);
	PLAYER_LIST[self:GetUserId()]=self
end;

function M.dtor(self)
	PLAYER_LIST[self:GetUserId()]=nil
	M.super.dtor(self);
end;

--已有数据，但是客户端player还未设置数据
function M.OnLogin(self)
	

	self.DataBase= self:GetComponent(ServerOHOH.load.Server_PlayerDataComponent());
	self:AddComponent(_G.CommonOHOH.load.Common_GamePassCtrl()); 

	self:AddComponent(ServerOHOH.load.Server_ItemCtrl())

	self:AddComponent(ServerOHOH.load.Server_Leaderboard())

end;


function M.SynchDataToClient(self,...)
	local keys={...}
	local netData={}
	for _,v in pairs(keys) do
		self.UserDataEvent:Action(v)
		local data=self.DataBase:GetData(v)
		assert(data~=nil,"Data==nil,Error Key="..v)
		if type(data)=="table" then
			netData[v]=data.__real
		else
			netData[v]=data
		end
	end
	ServerEventManager:Fire(self, EventList.synch_userdata, netData);
end


function M.FireMsgDataToClient(self,...)
	local keys={...}
	local netData={}
	for _,v in pairs(keys) do
		local data=self.DataBase:GetData(v)
		assert(data~=nil,"Data==nil,Error Key="..v)
		if type(data)=="table" then
			netData[v]=data.__real
		else
			netData[v]=data
		end
	end
	ServerEventManager:Fire(self, EventList.synch_userdata, netData);
end

--客户端已有数据
function M.OnLoginOver(self)


	_G.CommonOHOH.load.PlayerTool().AddCommonComponent(self)
	
	
	
end;




function M.OnCharacterIn(self, Character)
	self.CharacterEnterEvent:Action(Character)

end;

function M.OnCharacterOut(self, Character)

end;

-- function M.GetPetRandomLucky(self)
-- 	return self.LuckyCtrl:Lucky()
-- end;

--聊天
-- local ChatService = require(game:GetService("ServerScriptService"):WaitForChild("ChatServiceRunner").ChatService); 
function M.SetPlayerChat(self, tag)

	-- local Speaker = nil;
	-- while Speaker == nil do
	-- 	Speaker = ChatService:GetSpeaker(self.main_inst.Name);
	-- 	task.wait(1)	
	-- end;
	-- Speaker:SetExtraData("Tags",{{
	-- 	TagText = "SVIP",
	-- 	TagColor = Color3.fromRGB(255, 125, 0)
	-- }});
	-- Speaker:SetExtraData("ChatColor", Color3.fromRGB(255, 255, 0));
	-- Speaker:SetExtraData("NameColor", Color3.fromRGB(0, 251, 255));
end;

local TeleportService = game:GetService("TeleportService")

function M.IdledOutTimeTeleport(self,workState)
	self:AddProcess(function()
		local attemptIndex = 0
		local success, result -- define pcall results outside of loop so results can be reported later on	
		local teleportOptions = Instance.new("TeleportOptions")
		local teleportData = {
			placeId = game.PlaceId,
			jobId = game.JobId,
			work=workState
		}
		teleportOptions:SetTeleportData(teleportData)
		repeat
			success, result = pcall(function()
				return TeleportService:TeleportAsync(game.PlaceId,{self.main_inst},teleportOptions) -- teleport the user in a protected call to prevent erroring
			end)
			attemptIndex += 1
			if not success then
				self:Wait(1)
			end
		until success or attemptIndex == 5 -- stop trying to teleport if call was successful, or if retry limit has been reached
		if not success then
			warn(result) -- print the failure reason to output
		end
	end)
end

ServerOHOH.load.ServerFunctionManager():SetHandle(ServerOHOH.load.FunctionList().create_character, function(player)
	player:CreateCharacter();
end);

-- ServerOHOH.load.ServerFunctionManager():SetHandle(ServerOHOH.load.FunctionList().teleport_agin, function(player,workState)
-- 	player:IdledOutTimeTeleport(workState);
-- end);

ServerOHOH.event_manager:RegistHandle(ServerOHOH.event_list.UpdDone, function(player)
	assert(player,"player==nil")
    local DataBase=player:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
	DataBase:SetData("upd",_G.CommonOHOH.config.Game.Upd.id)
end);

return M;
